package com.healthcloud.service.RegionalDisease.impl;


import com.healthcloud.constants.PagineConsts;
import com.healthcloud.entity.HlHospitalEntity;
import com.healthcloud.entity.RegionalDisease.BaseLargeareaEntity;
import com.healthcloud.service.RegionalDisease.AreaGroupService;
import com.healthcloud.service.RegionalDisease.BaseAreaCodeService;
import com.healthcloud.service.RegionalDisease.BaseLargeareaService;
import com.healthcloud.utils.PageParam;
import com.healthcloud.utils.RedisUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yucl on 2017/11/9.
 */
@Service("areaGroupService")
public class AreaGroupServiceImpl implements AreaGroupService {


//    @Autowired
//    private TitleGroupDao titleGroupDao;

    @Autowired
    private BaseAreaCodeService baseAreaCodeService;

    @Autowired
    private BaseLargeareaService baseLargeareaService;

    @Autowired
    private RedisUtils redisUtils;

//    @Override
//    public List<PpPatientBaseEntity> queryPersonsByAreaCode(Long areaCode, String level, String fromMonth, String toMonth) {
//        List<HlHospitalEntity> list = queryByAreaCode(areaCode,level);
//
//        if(list == null || list.isEmpty()){
//            return null;
//        }
//
//        List<Long> hosp_id = new ArrayList<>();
//        for(HlHospitalEntity hlHospitalEntity : list){
//
//            hosp_id.add(hlHospitalEntity.getId());
//        }
//
//        if(StringUtils.isBlank(fromMonth)){
//
//            fromMonth = "1970-01";
//        }
//
//        if(StringUtils.isBlank(toMonth)){
//
//            toMonth = "3000-12";
//        }
//
//        List<PpPatientBaseEntity> ppPatientBaseEntityList =  titleGroupDao.groupAreaTestPersons(hosp_id,fromMonth,toMonth);
//
//        //加入缓存，失效时间为一个小时
//        if(ppPatientBaseEntityList !=null && !ppPatientBaseEntityList.isEmpty()) {
//            redisUtils.setList(Constant.REDIS_PATIENT, ppPatientBaseEntityList, 60 * 60);
//        }
//
//        //加入缓存
//        return ppPatientBaseEntityList;
//    }

    @Override
    public List<HlHospitalEntity> queryByAreaCode(Long areaCode, String level) {

        List<HlHospitalEntity> list = new ArrayList<>();

        if(StringUtils.isBlank(level) || "0".equals(level)){
            //大区

            if(-1 == areaCode){

                PageParam pageParam = new PageParam();
                pageParam.setiDisplayLength(PagineConsts.iDisplayLength);
                pageParam.setiDisplayStart(PagineConsts.iDisplayStart);
                List<BaseLargeareaEntity> baseLargeareaEntityList = baseLargeareaService.queryList(null,pageParam);
                for(BaseLargeareaEntity baseLargeareaEntity: baseLargeareaEntityList){

                    list.addAll(baseAreaCodeService.queryByLargeAreaCode(Long.valueOf(baseLargeareaEntity.getLargeAreaCode())));
                }
            }else {
                list = baseAreaCodeService.queryByLargeAreaCode(areaCode);
            }

        }else{
            //联动区域
            list = baseAreaCodeService.queryByAreaCode(areaCode);
        }

        return list;
    }
}
